package com.example.tao.mycar.activity;

import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import com.example.tao.mycar.MyActionBarActivity;
import com.example.tao.mycar.MyApplication;
import com.example.tao.mycar.R;
import com.example.tao.mycar.database.OperateDBUtils;
import com.example.tao.mycar.fragment.HeightViewFragment;
import com.example.tao.mycar.fragment.MileageFragment;
import com.example.tao.mycar.fragment.MileageTipsFragment;
import com.example.tao.mycar.fragment.WeightTipsFragment;
import com.example.tao.mycar.fragment.WeightViewFragment;
import com.example.tao.mycar.utils.RequestUtils;

import java.util.ArrayList;
import java.util.List;

public class UserInfoActivity extends MyActionBarActivity implements HeightViewFragment.OnFragmentInteractionListener,
        WeightViewFragment.OnFragmentInteractionListener, MileageFragment.OnFragmentInteractionListener,
        WeightTipsFragment.OnFragmentInteractionListener, MileageTipsFragment.OnFragmentInteractionListener{

    private static final int[] TABS = {R.string.main_tab_height, R.string.main_tab_weight, R.string.main_tab_mileage};
    private List<Fragment> mFragments;
    private ArrayList<View> mDots;

    ViewPager mViewpager;
    LinearLayout mFragmentLinearLayout;

    private int mBabyHeightColor;
    private int mCurveInfoColor;
    private int mPosition = 0;
    private int oldPosition;

    private MyFragmentPagerAdapter mPagerAdapter;

    private WeightTipsFragment mWeightTipsFragment;
    private MileageTipsFragment mMileageTipsFragment;

    private ContentObserver contentObsever = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            super.onChange(selfChange, uri);
            if (uri.getPath().contains(OperateDBUtils.HEIGHT)) {
                RequestUtils.mUserHeightChangeListener.userHeightChange();
            } else if (uri.getPath().contains(OperateDBUtils.WEIGHT)) {
                RequestUtils.mUserWeightChangeListener.userWeightChange();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_user_info);
        //Toolbar mToolbar = (Toolbar)findViewById(R.id.custom_actionbar);
        setToolBar("", false);

        Log.d(MyApplication.TAG, "create UserInfoActivity");

        mViewpager = (ViewPager)findViewById(R.id.fragment_viewpager);

        mBabyHeightColor = getApplication().getResources().getColor(R.color.light_red_color);
        mCurveInfoColor = getApplication().getResources().getColor(R.color.light_red_color);

        mDots = new ArrayList<>();
        mDots.add(findViewById(R.id.dot1));
        mDots.add(findViewById(R.id.dot2));
        mDots.add(findViewById(R.id.dot3));

        mFragments = new ArrayList<>();
        mFragments.add(HeightViewFragment.newInstance("", ""));
        mFragments.add(WeightViewFragment.newInstance("", ""));
        mFragments.add(MileageFragment.newInstance("", ""));
        mPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments);
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mDots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);
                mDots.get(position).setBackgroundResource(R.drawable.dot_focused);
                oldPosition = position;

                switch (position) {
                    case 0:
                        mPosition = 0;
                        onResume();
                        break;
                    case 1:
                        mPosition = 1;
                        onResume();
                        break;
                    case 2:
                        mPosition = 2;
                        onResume();
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mViewpager.setOffscreenPageLimit(3);
        mViewpager.setAdapter(mPagerAdapter);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            int position = bundle.getInt("position");
            mViewpager.setCurrentItem(position);
            oldPosition = position;
            mDots.get(position).setBackgroundResource(R.drawable.dot_focused);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        getContentResolver().registerContentObserver(OperateDBUtils.HEIGHT_URI, true, contentObsever);
        getContentResolver().registerContentObserver(OperateDBUtils.WEIGHT_URI, true, contentObsever);

        mMileageTipsFragment = MileageTipsFragment.newInstance("", "");
        mWeightTipsFragment = WeightTipsFragment.newInstance("", "");

        switch (mPosition) {
            case 0 :
                setToolBar(getResources().getString(R.string.main_tab_height), true);
                getSupportFragmentManager().beginTransaction().replace(R.id.tips, mWeightTipsFragment).commitAllowingStateLoss();
                break;
            case 1 :
                setToolBar(getResources().getString(R.string.main_tab_weight), true);
                getSupportFragmentManager().beginTransaction().replace(R.id.tips, mWeightTipsFragment).commitAllowingStateLoss();
                break;
            case 2 :
                setToolBar(getResources().getString(R.string.main_tab_mileage), true);
                getSupportFragmentManager().beginTransaction().replace(R.id.tips, mMileageTipsFragment).commitAllowingStateLoss();
                break;
            default:
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getContentResolver().unregisterContentObserver(contentObsever);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }


    class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;
        private int[] mTabText;

        public MyFragmentPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        public MyFragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
            super(fragmentManager);
            mFragments = fragments;
        }

        public MyFragmentPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments, int[] tabText) {
            super(fragmentManager);
            mFragments = fragments;
            mTabText = tabText;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
